package util;

import domain.Student;
import org.junit.Test;

import java.sql.*;

public class DBUtil {
    @Test
    public void execute(){
        try {
            //注册mysql驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取数据库链接，第一个参数是连接字符串，第二个参数是用户名，第三个参数是密码
            Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Asia/Shanghai","root","");
           //Connection conn= DriverManager.getConnection("jdbc:mysql://10.10.67.142:3306/test?useSSL=false&serverTimezone=Asia/Shanghai","root","root");
            //创建一个传输sql语句的statement对象
            Statement statement=conn.createStatement();
            //创建一个接受select结果的ResultSet对象rs
           // ResultSet rs=statement.executeQuery("select  * from student");
            /*//循环显示rs对象中的数据；rs。next（）遍历的针
            while(rs.next()){
                System.out.print(rs.getString("name")+"   ");
                System.out.println(rs.getString("clazz"));
            }*/
           /* String sql="INSERT INTO student(id,name,clazz) VALUES (116,'pete','19soft4')";
            statement.execute(sql);*/
            /*String sql="INSERT INTO course (id,name,teacher_id) VALUES (5,'lichuanjun','205')";
            statement.execute(sql);*/
            ResultSet rs=statement.executeQuery("select  * from course");

            //循环显示rs对象中的数据；rs。next（）遍历的针
            Student student;
            
            while(rs.next()){
                System.out.print(rs.getString("teacher_id")+"   ");
                System.out.println(rs.getString("name"));
                System.out.println(rs.getString("name"));
               /* student=new Student();
                student.setName(rs.getString("name"));
                students.add(student);*/
                
            }
            //关闭链接释放资源
            rs.close();
            statement.close();
            conn.close();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }


    }
}
